<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Question extends Model
{
    protected $table      = 'question';
    protected $primaryKey = 'qs_id';
    protected $fillable   = ['paper_id','qs_name','qs_score','qs_type','option_a','option_b','option_c','option_d','qs_answer','qs_remark'];
    use SoftDeletes;
    protected $dates      = ['deleted_at'];

    //试题的答案用二级制表示  在模板中调用 所以用静态属性
    public static $answer = [
        '1'=>'A',
        '2'=>'B',
        '3'=>'AB',
        '4'=>'C',
        '5'=>'AC',
        '6'=>'BC',
        '7'=>'ABC',
        '8'=>'D',
        '9'=>'AD',
        '10'=>'BD',
        '11'=>'ABD',
        '12'=>'CD',
        '13'=>'ACD',
        '14'=>'BCD',
        '15'=>'ABCD',
    ];

    //判断用户答案是否正确并得分
    public function getScore($result){
        //用户的答案与正确答案做与运算
        $res = ($this->qs_answer & $result);//qs_answer 是question表里面的字段
        //判断  当用户的答案与计算之后的答案相同 并且 用户答案与正确
        //如果用户输入的答案和标准答案相同 则全对
        if($result == $this->qs_answer){
            return ['score'=>$this->qs_score,'right'=>'对'];
        }elseif($res == $result){//半对
            return ['score'=>$this->qs_score/2,'right'=>'半对'];
        }else{//错误
            return ['score'=>0,'right'=>'错'];
        }

    }
    //同一场考试 每个学生考的试卷 和 答案是一一对应的关系
    public function answerquestion(){
        return $this->hasOne('App\Http\Models\AnswerQuestion','qs_id','qs_id');
    }

}
